﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ZoneBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILocationDA LotDA = DataAccess.CreateLocationDA();
        private PubParasInfo pubParasInfo;
        private static readonly IZoneDA zoneDA = DataAccess.CreateZoneDA();

        public ZoneBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ZoneBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> zoneIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<ZoneInfo> zoneInfos = new List<ZoneInfo>();
                ZoneInfo zoneInfo = null;
                foreach (string id in zoneIDS)
                {
                    zoneInfo = zoneDA.GetZoneByID(dataBase, tran, id, this.dataBaseID[0]);
                    zoneInfos.Add(zoneInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (zoneDA.DeleteZone(dataBase, tran, zoneIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < zoneIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<ZoneInfo>(dataBase, tran, zoneInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ZONE", this.dataBaseID[0]);
                        temp = temp + zoneIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return zoneDA.GetAllZone(this.dataBaseID[0]);
        }

        public object GetInfoByID(string zoneID)
        {
            return zoneDA.GetZoneByID(zoneID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            ZoneQueryEntity zoneQuery = obj as ZoneQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(zoneQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + zoneQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(zoneQuery.ZoneID))
            {
                sqlWhere.Add(" Zone_ID " + zoneQuery.ZoneID);
            }
            if (!string.IsNullOrEmpty(zoneQuery.MaxCube))
            {
                sqlWhere.Add(" MAX_CUBE " + zoneQuery.MaxCube);
            }
            if (!string.IsNullOrEmpty(zoneQuery.MaxWeight))
            {
                sqlWhere.Add(" MAX_WEIGHT " + zoneQuery.MaxWeight);
            }
            if (!string.IsNullOrEmpty(zoneQuery.MaxCasecnt))
            {
                sqlWhere.Add(" MAX_CASECNT " + zoneQuery.MaxCasecnt);
            }
            return zoneDA.GetZoneByQueryList(sqlWhere, zoneQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string zoneID = string.Empty;
            try
            {
                ZoneInfo zone = obj as ZoneInfo;
                if (!string.IsNullOrEmpty(zone.PickToLoc) && !LotDA.CheckLocationIDUnique(zone.PickToLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2310_001", this.pubParasInfo.UserLanguage, new List<string> { zone.PickToLoc });
                }
                if (!string.IsNullOrEmpty(zone.InLoc) && !LotDA.CheckLocationIDUnique(zone.InLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2310_002", this.pubParasInfo.UserLanguage, new List<string> { zone.InLoc });
                }
                if (!string.IsNullOrEmpty(zone.OutLoc) && !LotDA.CheckLocationIDUnique(zone.OutLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2310_003", this.pubParasInfo.UserLanguage, new List<string> { zone.OutLoc });
                }
                if (zoneDA.CheckZoneIDUnique(zone.ZoneID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2340_005", this.pubParasInfo.UserLanguage, new List<string> { zone.ZoneID });
                }
                zone.UpdatedBy = zone.CreatedBy;
                zone.CreatedDate = DateTime.Now;
                zone.UpdatedDate = DateTime.Now;
                if (zoneDA.InsertZone(zone, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<ZoneInfo>(null, zone, AuditAction.Insert, this.pubParasInfo.UserID, "ZONE", this.dataBaseID[0]);
                    zoneID = zone.ZoneID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return zoneID;
        }

        public string Update(object obj)
        {
            string zoneID = string.Empty;
            try
            {
                ZoneInfo zone = obj as ZoneInfo;
                if (!string.IsNullOrEmpty(zone.PickToLoc) && !LotDA.CheckLocationIDUnique(zone.PickToLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2310_001", this.pubParasInfo.UserLanguage, new List<string> { zone.PickToLoc });
                }
                if (!string.IsNullOrEmpty(zone.InLoc) && !LotDA.CheckLocationIDUnique(zone.InLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2310_002", this.pubParasInfo.UserLanguage, new List<string> { zone.InLoc });
                }
                if (!string.IsNullOrEmpty(zone.OutLoc) && !LotDA.CheckLocationIDUnique(zone.OutLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2310_003", this.pubParasInfo.UserLanguage, new List<string> { zone.OutLoc });
                }
                ZoneInfo oldZoneInfo = zoneDA.GetZoneByID(zone.ZoneID, this.dataBaseID[0]);
                if (zoneDA.UpdateZone(zone, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<ZoneInfo>(oldZoneInfo, zone, AuditAction.Update, this.pubParasInfo.UserID, "ZONE", this.dataBaseID[0]);
                    zoneID = zone.ZoneID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return zoneID;
        }
    }
}

